package com.jy.app.lib.view.uilistview;

import com.alibaba.fastjson.TypeReference;
import com.jy.app.lib.adapter.CommonListAdaptrer;
import com.jy.app.lib.utils.DateTimeUtils;
import com.jy.app.lib.utils.JsonUtil;
import com.jy.app.lib.utils.ToastUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class Pagin<T> {
    public TypeReference<T> type = null;
    private List<T> data; //数据
    private int page = 1; //页数
    private int pageSize = 20; //每页条数
    private UILoadListView listview;
    private NetWorkRequest<T> onNetWorkRequest;
    private HttpNetWork httpNetWork;
    private CommonListAdaptrer adaptrer;
    private List<T> itemData;
    private long homePageTime = 0;
    private String refreshTime;

    public Pagin(UILoadListView listview) {
        this.type = new TypeReference<T>(){};
        data = new ArrayList<>();
        this.listview = listview;
        bindEvents();
    }

    public Pagin(UILoadListView listview, CommonListAdaptrer adaptrer, HttpNetWork httpNetWork) {
        this(listview);
        this.adaptrer = adaptrer;
        this.httpNetWork = httpNetWork;
        listview.setAdapter(adaptrer);
        start();
    }

    public void start() {
        homePage();
    }

    private void upDateData() {
        if (page == 1) {
            data.clear();
        }
        if (itemData != null)
            for (int i = 0; i < itemData.size(); i++) {
                data.add(itemData.get(i));
            }
        setListViewState();
        if (onNetWorkRequest != null)
            onNetWorkRequest.onAllData(data);
        if (adaptrer!=null)
            adaptrer.refreshAdapter(data);

    }

    public Type getSonType(){
        Type type_ = ((ParameterizedType) this.type.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return  type_;
    }

    public void setItemData(List<T> itemData) {
        this.itemData = itemData;
        upDateData();
    }

    public void setItemData(String str) {
        try {
            this.itemData = (List) JsonUtil.parseObject(str, getSonType().getClass());
        } catch (Exception e) {
            e.printStackTrace();
        }
        upDateData();
    }

    public void setShowLoading(){
        listview.loadWarnNetLoading();
    }

    private void setListViewState() {
            listview.stopLvRefresh();
            if (page == 1) {
                listview.hideLoadingLayout();
            }
            if (page == 1 && data.size() == 0) {
                listview.loadWarnNoResult();
            } else if (itemData.size() < pageSize) {
                listview.hideMoreView();
            } else if (itemData.size() >= pageSize) {
                listview.showMoreView();
            }
    }

    public List<T> getData() {
        return data;
    }

    public void setOnNetWorkRequest(NetWorkRequest<T> onNetWorkRequest) {
        this.onNetWorkRequest = onNetWorkRequest;
    }

    public void setError() {
        if (page == 1) {
            listview.loadWarnNetFail();
        } else {
            ToastUtils.toastShort("网络超时");
            listview.stopLvLoadMore();
        }
    }

    public void setError(String error) {
        if (page == 1) {
            listview.loadWarnNetFail();
            listview.setErrorText(error);
        } else {
            ToastUtils.toastShort("网络超时");
            listview.stopLvLoadMore();
        }
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setPage(int page) {
        this.page = page;
    }

    private void nextPage() {
        page++;
        if (onNetWorkRequest!=null) onNetWorkRequest.OnNetWorkRequest();
        if (httpNetWork!=null) httpNetWork.OnNetWorkRequest(this);
    }

    private void homePage() {
        refreshTime = DateTimeUtils.dateToString(new Date(), DateTimeUtils.EnumDateFmt.yyyyMMddHHmmss);
        page = 1;
        listview.mListView.hideMenu();
        if (data == null || data.size() <= 0)
            listview.loadWarnNetLoading();
        if (onNetWorkRequest!=null) onNetWorkRequest.OnNetWorkRequest();
        if (httpNetWork!=null) httpNetWork.OnNetWorkRequest(this);
    }

    public int getPage() {
        return page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPaginParams(Map<String, String> params) {
        if (params != null) {
            params.put("page", page + "");
            params.put("pageSize", pageSize + "");
            params.put("refreshTime", refreshTime + "");
        }
    }

    private void bindEvents() {
        listview.setOnLoadEventListener(new UILoadListView.OnLoadEventListener() {
            @Override
            public void onListRefresh() { //下拉刷新的事件监听
                homePage();
            }

            @Override
            public void onListLoadMore() {//上拉加载的事件监听
                nextPage();
            }

            @Override
            public void onLoadingRefresh() {//加载数据或访问网络失败提示重新刷新的事件监听
                homePage();
            }

            @Override
            public void onNoButtonClickRefresh() {//在点击屏幕重新加载和刷新的背景下的事件监听
                homePage();
            }
        });
    }

    public interface NetWorkRequest<T> {
        public void OnNetWorkRequest();

        public void onAllData(List<T> allData);
    }

    public interface HttpNetWork {
        public void OnNetWorkRequest(Pagin pagin);
    }
}
